/**
 * $Id$
 * Copyright 2011-2014 Oak Pacific Interactive. All rights reserved.
 */
package com.malphi.framework.flash.result;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 要回传给flash的返回数据解析成json
 * 
 * @author <a href="mailto:ruofei.ma@renren-inc.com">ma ruofei</a>
 * @version 1.0 2011-11-29 下午03:53:51
 * @since 1.0
 */
public class ResultDataParser {

    public static final String PRIMITIVE_DATA_KEY = "\"value\"";

    /**
     * javabean转换成json
     * 
     * @param javaBean
     * @return
     */
    public static String bean2Json(Object javaBean) {
        return JSONObject.fromObject(javaBean).toString();
    }

    /**
     * 基本类型转json 包括boolean
     * 
     * @param data
     * @return
     */
    public static String primitive2Json(Object data) {
        return JSONObject.fromObject(formatJsonPattern(data)).toString();
    }

    /**
     * 字符串转json
     * 
     * @param str
     * @return
     */
    public static String string2Json(String str) {
        str = "\"" + str + "\"";
        return JSONObject.fromObject(formatJsonPattern(str)).toString();
    }

    /**
     * list 转json 基本类型和bean都适用
     * 
     * @param list
     * @return
     */
    public static String list2Json(List<?> list) {
        if (list == null || list.size() == 0) return emptyJson();
        JSONArray array = JSONArray.fromObject(list);
        return array.toString();
    }

    /**
     * map 转json 通用
     * @param map
     * @return
     */
    public static String map2Json(Map<?, ?> map) {
        if (map == null || map.size() == 0) return emptyJson();
        JSONArray array = new JSONArray();
        for(Map.Entry<?,?> entry : map.entrySet()){
            JSONObject o = new JSONObject();
            o.element(String.valueOf(entry.getKey()),entry.getValue());
            array.add(o);
        }
        return array.toString();
    }

    private static String formatJsonPattern(Object data) {
        StringBuilder sb = new StringBuilder("{").append(PRIMITIVE_DATA_KEY).append(":").append(
                data).append("}");
        return sb.toString();
    }

    private static String emptyJson() {
        return "{}";
    }

//    public static void main(String[] args) {
//                List<Bean> list = new ArrayList<Bean>();
//                list.add(new Bean(1,"sdf"));
//                list.add(new Bean(2,"sdf12"));
//                System.out.println(list2Json(list));
//        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
//        map.put(1,1);
//        map.put(2,2);
//        System.out.println(map2Json(map));
//        //        System.out.println(primitive2Json(true));
//    }

    /**
     * json to bean bean不能有有参的构造函数 get/set 属性名和json对应
     * 
     * @param json
     * @param beanClass
     * @return
     */
    public static Object json2bean(String json, Class<?> beanClass) {
        JSONObject jsonObject = JSONObject.fromObject(json);
        return JSONObject.toBean(jsonObject, beanClass);
    }
}
